package com.project.nimsen.web.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.jdbc.utils.DateUtils;
import com.project.jdbc.utils.Page;
import com.project.jdbc.web.BaseMVCAction;
import com.project.nimsen.common.web.LoginUser;
import com.project.nimsen.dto.BComBillDto;
import com.project.nimsen.service.IBComBillService;
import com.project.nimsen.util.ValdataUtil;
/**
 * 投诉单Control
 * */
@Controller
@RequestMapping(value ="/sys/comBill")
public class BComBillControl extends BaseMVCAction {
	@Resource
	IBComBillService bComBillService;
	//跳转到投诉添加页面
	@RequestMapping(value ="/addView")
	public String comBillView(){
		return "/WEB-INF/sys/combill/add";
	}
	//跳转到待办信息页面
	@RequestMapping(value ="/doView")
	public String doView(){
		this.getRequest().setAttribute("nowTime", DateUtils.convertDate2String(new Date()));
		return "/WEB-INF/sys/combill/do";
	}
	//跳转到待办信息页面
	@RequestMapping(value ="/alreadyView")
	public String alreadyView(){
		return "/WEB-INF/sys/combill/already";
	}
	//处理流程
	@RequestMapping(value="/doAction")
	@ResponseBody
	public Map<String, Object> doAction(){
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		String com_bill_id = getParameter("com_bill_id");//单据编号
		if(StringUtils.isBlank(com_bill_id)){
			map.put("message", "无效的参数信息");
			return map;
		}
		String cir_bill_id = getParameter("cir_bill_id");//流转单编号
		if(StringUtils.isBlank(cir_bill_id)){
			map.put("message", "无效的参数信息");
			return map;
		}
		BComBillDto dto = new BComBillDto();
		LoginUser loginUser = LoginUser.getCurr();
		dto.setUserid(loginUser.getUserid());
		dto.setUnitid(loginUser.getUnitid());
		dto.setBill_id(com_bill_id);
		dto.setCir_unit_id(getParameter("actionUnit"));//处理部门
		dto.setCir_bill_id(cir_bill_id);
		dto.setEndTime(getDateParameter("endTime"));//截止时间
		dto.setContext(getParameter("context"));//内容信息
		dto.setOther_type(getParameter("other_type"));//处理类型
		dto.setAction_bill_id(getParameter("action_bill_id"));//处理单编号
		dto.setExa_bill_id(getParameter("exa_bill_id"));//审核单编号
		//答复信息
		String satisfy = getParameter("satisfy");
		if("0".equals(satisfy)){
		dto.setSatisfy("0");
		}else if("50".equals(satisfy)){
			dto.setSatisfy("1");
		}else if("100".equals(satisfy)){
			dto.setSatisfy("2");
		}
		dto.setRemark(getParameter("remark"));
		try {
			if(bComBillService.saveByDto(dto))
				map.put("success", true);
			else
				map.put("message", "发送失败");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	//得到处理的信息
	@RequestMapping(value="/doActionInfo")
	@ResponseBody
	public Map<String, Object> doActionInfo(){
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("success", false);
		
		String id = getParameter("id");
		if(StringUtils.isBlank(id)){
			map.put("message", "无效的参数信息");
			return map;
		}
		String src_unit_id = getParameter("src_unit_id");
		if(StringUtils.isBlank(src_unit_id)){
			map.put("message", "无效的参数信息");
			return map;
		}
		String bill_id = getParameter("bill_id");
		if(StringUtils.isBlank(bill_id)){
			map.put("message", "无效的参数信息");
			return map;
		}
		BComBillDto dto = getDto();
		dto.setId(id);
		dto.setSrc_unit_id(src_unit_id);
		dto.setBill_id(bill_id);
		try {
			Map<String, Object> cir_info = bComBillService.findCirInfoByDto(dto);
			if(cir_info==null||CollectionUtils.isEmpty(cir_info.keySet())){
				map.put("message", "没有找到相应的信息");
				return map;
			}
			map.put("success", true);
			map.put("data", cir_info);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		
		return map;
	}
	@RequestMapping(value="/dosearch")
	@ResponseBody
	public Map<String, Object> dosearch(){
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", 0);
		BComBillDto dto = getDto();
		dto.setDanbanis(false);
		try {
			map = search(map, dto);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	//查询方法
	private Map<String, Object> search(Map<String, Object> map, BComBillDto dto) throws Exception{
		List<Map<String, Object>> rows = bComBillService.findoByDto(dto);
		if(CollectionUtils.isEmpty(rows)){
			map.put("rows", "");
			return map;
		}
		for(Map<String, Object> row : rows){
			Date create_time = (Date)row.get("BILL_TIME");
			row.put("BILL_TIME", DateUtils.convertDate2String(create_time));
			
			String active = row.get("ACTIVE").toString();
			if("0".equals(active)){
				row.put("ACTIVE_NAME", "未办理");
			}else{
				row.put("ACTIVE_NAME", "已办理");
			}
			Object obj_action_date = row.get("ACTION_END_TIME");
			if(obj_action_date!=null){
				row.put("ACTION_END_TIME", DateUtils.convertDate2String((Date)obj_action_date));
			}else{
				row.put("ACTION_END_TIME", "");
			}
		}
		map.put("rows", rows);
		map.put("total", dto.getPage().getTotalCount());
		return map;
	}
	@RequestMapping(value="/searchdo")
	@ResponseBody
	public Map<String, Object> searchdo(){
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", 0);
		BComBillDto dto = getDto();
		try {
			map = search(map, dto);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	//得到基础查询类
	private BComBillDto getDto(){
		BComBillDto dto = new BComBillDto();
		//分页信息
		Integer pageIndex = getIntegerParameter("page");
		if(pageIndex!=null){
			Page page = new Page(pageIndex);
			page.setPageSize(getIntegerParameter("rows"));
			dto.setPage(page);
		}
		LoginUser loginUser = LoginUser.getCurr();
		dto.setUserid(loginUser.getUserid());
		dto.setUnitid(loginUser.getUnitid());
		return dto;
	}
	//查询页面
	@RequestMapping(value ="/search")
	@ResponseBody
	public Map<String, Object> search(){
		BComBillDto dto = getDto();
		
		//返回JSON信息
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", 0);
		try {
			List<Map<String, Object>> rows = bComBillService.findByDto(dto);
			if(CollectionUtils.isEmpty(rows))
				return null;
			for(Map<String, Object> row : rows){
				Date create_time = (Date)row.get("CREATE_TIME");
				row.put("CREATE_TIME", DateUtils.convertDate2String(create_time));
				
				String type = row.get("TYPE").toString();
				if("0".equals(type)){
					row.put("TYPE", "投诉单");
				}
				
				String active = row.get("ACTIVE").toString();
				if("0".equals(active)){
					row.put("ACTIVE", "新建");
				}else if("1".equals(active)){
					row.put("ACTIVE", "处理中");
				}else if("2".equals(active)){
					row.put("ACTIVE", "已完成");
				}else{
					row.put("ACTIVE", "已完成");
				}
				Object id = row.get("ID");
				if(id!=null){
					Map<String, Object> unitinfo = bComBillService.findLastUnitInfo(id.toString());
					if(unitinfo==null){
						row.put("UNIT_NAME", "已完成");
					}else{
						row.put("UNIT_NAME", unitinfo.get("UNIT_NAME"));
					}
				}
			}
			map.put("rows", rows);
			map.put("total", dto.getPage().getTotalCount());
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
	//添加一个投诉单
	@RequestMapping(value ="/addComBill")
	@ResponseBody
	public Map<String, Object> addComBill(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		String name = getParameter("name");
		if(StringUtils.isBlank(name)){
			map.put("message", "姓名为必填项");
			return map;
		}
		String mphone = getParameter("mphone");
		if(!ValdataUtil.valphone(mphone)){
			map.put("message", "电话格式不正确");
			return map;
		}
		String address = getParameter("address");
		if(StringUtils.isBlank(address)){
			map.put("message", "地址为必填项");
			return map;
		}
		String resons = getParameter("resons");
		if(StringUtils.isBlank(resons)){
			map.put("message", "原因为必填项");
			return map;
		}
		
		LoginUser loginUser = LoginUser.getCurr();
		BComBillDto dto = new BComBillDto();
		dto.setUserid(loginUser.getUserid());
		dto.setUnitid(loginUser.getUnitid());
		dto.setAddress(address);
		dto.setContext(getParameter("context"));
		dto.setCustName(name);
		dto.setPhone(mphone);
		dto.setResons(resons);
		try {
			if(bComBillService.saveByDto(dto)){
				map.put("success", true);
				map.put("message", "添加成功");
			}else
				map.put("message", "添加失败");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return map;
	}
}
